package fragments;

import java.util.ArrayList;
import java.util.List;

import model.HeroCombo;
import model.HeroComboFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dotalanecombos.R;

public class ComboFragment extends Fragment {

	public static final String EXTRA_COMBO = null;

	private HeroCombo heroCombo;
	private List<String> arrayCombos = new ArrayList<String>();// = new
																// String[3]; //
																// could result
																// in a
	// nullPointer
	private ListView comboListView;
	private ImageView mainHeroPortrait;
	private TextView mainHeroName;
	private TextView mainHeroRole;

	public static ComboFragment newInstance(String mainHeroName) {
		Bundle args = new Bundle();
		args.putSerializable(EXTRA_COMBO, mainHeroName);

		ComboFragment comboFragment = new ComboFragment();
		comboFragment.setArguments(args);

		Log.d("DEBUG", "returning newInstance");
		return comboFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i("INFO", "HERE 1");
		// String heroName = (String)
		// getActivity().getIntent().getSerializableExtra(EXTRA_COMBO);
		String heroName = (String) getArguments().getSerializable(EXTRA_COMBO);
		heroCombo = HeroComboFactory.get(getActivity()).getHeroComboByMainHeroName(heroName);
		Log.i("INFO", "HERE 2: " + heroCombo.getCombo().size());

		// Loading combos - REMOVE

		for (int i = 0; i < heroCombo.getCombo().size(); i++) {
			arrayCombos.add(heroCombo.getCombo().get(i).getHeroName());// heroCombo.getCombo().get(i).getHeroName();
		}

		Log.i("INFO", "HERE 3: " + arrayCombos.size());
		for (int i = 0; i < arrayCombos.size(); i++) {
			Log.d("DEBUGGING", "testing: " + arrayCombos.get(i));
		}
		Log.i("INFO", "HERE 4");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_combo, container, false);

		comboListView = (ListView) v.findViewById(R.id.combos_list_view);
		comboListView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
				arrayCombos));

		mainHeroPortrait = (ImageView) v.findViewById(R.id.main_hero_portrait);
		int imageId = getResources().getIdentifier(heroCombo.getMainHero().getHeroName().toLowerCase(), "drawable",
				comboListView.getContext().getPackageName());
		mainHeroPortrait.setImageResource(imageId);

		mainHeroName = (TextView) v.findViewById(R.id.hero_name);
		mainHeroName.setText(heroCombo.getMainHero().getHeroName());

		mainHeroRole = (TextView) v.findViewById(R.id.hero_role);
		mainHeroRole.setText(heroCombo.getMainHero().getHeroRole());

		/*
		 * comboListView.setOnItemClickListener(new OnItemClickListener() {
		 * 
		 * @Override public void onItemClick(AdapterView<?> parent, View view,
		 * int position, long id) { Toast.makeText(getActivity(), "You Click " +
		 * arrayCombos[position], 1000).show(); } });
		 */

		return v;
	}

}
